﻿using CCWin;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MDF
{
    /// <summary>
    /// 设置管理窗口
    /// </summary>
    public partial class SZGL : CCSkinMain
    {
        //配置文件名
        private string m_xmlName = "MDFUser";

        private IO_xml sz_Xml;

        /// <summary>
        /// 初始化
        /// </summary>
        public SZGL()
        {
            InitializeComponent();

            sz_Xml = new IO_xml(m_xmlName);

            this.skinTabControl.SelectedIndex = 0;
            FullUserPage();
        }
        /// <summary>
        /// 切换选项卡时，填充对应的控件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void skinTabControl_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                switch (this.skinTabControl.SelectedIndex)
                {
                    case 0:
                        //用户页
                        FullUserPage();
                        break;
                    case 1:
                        //数据页
                        FillDataInfo();
                        break;
                    default:
                        break;
                }
            }
            catch (Exception)
            {

                throw;
            }
        }

        #region 用户页
        /// <summary>
        /// 填充用户页
        /// </summary>
        private void FullUserPage()
        {
            try
            {
                //获取已登陆用户信息
                string[] lastloadnames = sz_Xml.ReadChildNodesName("LastLoadInfo");
                foreach (string lastloadname in lastloadnames)
                {
                    this.UserName_TextBox.Text = lastloadname;
                    string password = sz_Xml.ReadValueByKey(lastloadname, "Password");
                    string check = sz_Xml.ReadValueByKey(lastloadname, "Check");
                    if (!string.IsNullOrEmpty(password))
                    {
                        this.Password_TextBox.Text = password;
                        this.RememberPassword.Checked = true;
                        if (check == "True")
                        {
                            this.AutomaticLogon.Checked = true;
                        }
                    }
                }
                //填充其他用户信息
                string[] othersnames= sz_Xml.ReadChildNodesName("UserInfo");
                foreach (string othersname in othersnames)
                {
                    if(othersname != this.UserName_TextBox.Text)
                    {
                        DataGridViewRow row = new DataGridViewRow();
                        row.CreateCells(this.OthersUser_GridView);

                        row.Cells[0].Value = othersname;
                        if(sz_Xml.ReadValueByKey(othersname, "Password") != null)
                        {
                            row.Cells[1].Value = "********";
                            row.Cells[2].Value = "True";
                        }
                        this.OthersUser_GridView.Rows.Add(row);

                    }
                }

            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 显示密码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ShowPassword_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                if (this.ShowPassword.Checked)
                {
                    this.Password_TextBox.UseSystemPasswordChar = false;
                }
                else
                {
                    this.Password_TextBox.UseSystemPasswordChar = true;
                }
                
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 自动登录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void AutomaticLogon_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                if (!this.RememberPassword.Checked)
                {
                    this.AutomaticLogon.Checked = false;
                }
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 记住密码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void RememberPassword_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                if (!this.RememberPassword.Checked)
                {
                    this.AutomaticLogon.Checked = false;
                }
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 修改当前登陆的用户信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Revise_Button_Click(object sender, EventArgs e)
        {
            try
            {
                if (!string.IsNullOrEmpty(UserName_TextBox.Text) && !string.IsNullOrEmpty(Password_TextBox.Text))
                {
                    //记录用户信息，是否记住密码
                    if (this.RememberPassword.Checked)
                    {
                        sz_Xml.WriteUsersInfo("UserInfo", UserName_TextBox.Text, Password_TextBox.Text, AutomaticLogon.Checked.ToString());
                        sz_Xml.WriteUsersInfo("LastLoadInfo", UserName_TextBox.Text, Password_TextBox.Text, AutomaticLogon.Checked.ToString());
                    }
                    else
                    {
                        sz_Xml.WriteUsersInfo("UserInfo", UserName_TextBox.Text, "", AutomaticLogon.Checked.ToString());
                        sz_Xml.WriteUsersInfo("LastLoadInfo", UserName_TextBox.Text, "", AutomaticLogon.Checked.ToString());
                    }
                }
                else
                {
                    MessageBox.Show("用户名或密码不能为空！","提示：");
                }
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 登出当前用户
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Logout_Button_Click(object sender, EventArgs e)
        {
            try
            {
                if (MessageBox.Show("登出后，下次将重新登陆~","提示：", MessageBoxButtons.OKCancel) == DialogResult.OK)
                {
                    if (!string.IsNullOrEmpty(UserName_TextBox.Text))
                    {
                        //删除上次登陆数据
                        if (sz_Xml.DeleteXml("LastLoadInfo", UserName_TextBox.Text))
                        {
                            //修改用户信息数据
                            sz_Xml.WriteUsersInfo("UserInfo", UserName_TextBox.Text, "", "");

                            this.UserName_TextBox.Text = "";
                            this.Password_TextBox.Text = "";
                            this.ShowPassword.Checked = false;
                            this.AutomaticLogon.Checked = false;
                            this.RememberPassword.Checked = false;

                            MessageBox.Show("登出成功！", "提示：");
                        }
                        else
                        {
                            MessageBox.Show("登出失败！", "提示：");
                        }
                    }
                }
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 删除选中的用户
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Delete_Button_Click(object sender, EventArgs e)
        {
            try
            {
                //获取用户名
                string userName = this.OthersUser_GridView.SelectedCells[0].OwningRow.Cells[0].Value.ToString();
                if(userName != null)
                {
                    //删除对应节点数据
                    if (sz_Xml.DeleteXml("UserInfo", userName))
                    {
                        //删除表中该行数据
                        this.OthersUser_GridView.Rows.Remove(this.OthersUser_GridView.SelectedCells[0].OwningRow);
                        MessageBox.Show("删除成功！", "提示：");
                    }
                    else
                    {
                        MessageBox.Show("删除失败！","提示：");
                    }
                    
                }
            }
            catch (Exception)
            {

                throw;
            }
        }
        #endregion

        #region 数据页
        /// <summary>
        /// 填充数据信息
        /// </summary>
        private void FillDataInfo()
        {
            try
            {
                this.FileTree_TreeView.Nodes.Add("文件目录");
                this.FileTree_TreeView.Nodes[0].Nodes.Add("配置文件").Name = "Sys";
                this.FileTree_TreeView.Nodes[0].Nodes.Add("数据文件").Name = "Data";
                this.FileTree_TreeView.Nodes[0].Nodes.Add("数据源文件").Name = "Source";
                //获取程序运行路径的文件夹和文件
                string path = Application.StartupPath;
                GetDicAndFiles(path + "\\Sys", this.FileTree_TreeView.Nodes[0].Nodes[0]);
                GetDicAndFiles(path + "\\Data", this.FileTree_TreeView.Nodes[0].Nodes[1]);
                GetDicAndFiles(path + "\\Source", this.FileTree_TreeView.Nodes[0].Nodes[2]);
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 递归获取文件夹和文件
        /// </summary>
        /// <param name="path">文件路径</param>
        /// <param name="node">要填充的节点</param>
        private void GetDicAndFiles(string path, TreeNode node)
        {
            try
            {
                if (Directory.Exists(path))
                {
                    //目录下的文件、文件夹集合
                    string[] dics = Directory.GetDirectories(path, "*", System.IO.SearchOption.AllDirectories);
                    string[] files = Directory.GetFiles(path);
                    //文件夹递归
                    for (int i = 0; i < dics.Length; i++)
                    {
                        //增加父节点
                        string dicName = Path.GetFileNameWithoutExtension(dics[i]);
                        node.Nodes.Add(dicName).Name = dicName;
                        GetDicAndFiles(dics[i], node.Nodes[i]);
                    }
                    //文件直接添加
                    foreach (string file in files)
                    {
                        string filename = Path.GetFileNameWithoutExtension(file);
                        node.Nodes.Add(filename).Name = filename;
                    }
                }
                else
                {
                    return;
                }
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 选择文件后填充信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FileTree_TreeView_AfterSelect(object sender, TreeViewEventArgs e)
        {
            try
            {
                if (e.Node.Level > 1)
                {
                    string path = Application.StartupPath + GetFilePath(e.Node)+".xml";
                    if (File.Exists(path))
                    {
                        this.Data_TextBox.Text = File.ReadAllText(path);
                    }
                    else
                    {
                        return;
                    }
                }
                else
                {
                    return;
                }
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 递归获取文件路径
        /// </summary>
        /// <param name="node">选中的节点</param>
        /// <returns>返回文件路径</returns>
        private string GetFilePath(TreeNode node)
        {
            try
            {
                string path = "\\" + node.Name;
                if (node.Level == 1)
                {
                    return path;
                }
                else
                {
                    path = GetFilePath(node.Parent) + path;
                }
                return path;
            }
            catch (Exception)
            {

                throw;
            }
        }
        #endregion
        /// <summary>
        /// 退出设置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Exit_Button_Click(object sender, EventArgs e)
        {
            try
            {
                this.DialogResult = DialogResult.OK;
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
